package cn.tom.config;


import cn.tom.dao.RoleDao;
import cn.tom.entity.Role;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

@Component
public class MyInitConfig {
    @Autowired
    RoleDao roleDao;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    ObjectMapper objectMapper;

    @PostConstruct
    public void init() throws JsonProcessingException {

        redisTemplate.delete("role");
        List<Role> roles = roleDao.findAllRole();
        for (Role role : roles) {
            Integer roleKey = role.getId();
            redisTemplate.opsForHash().put("role",roleKey,role);
        }
    }
}
